﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace LanqiQAwebapi.Filter
{
    [AttributeUsage(AttributeTargets.Method)]
    public class PreventDuplicateRequestsAttribute : Attribute, IFilterFactory
    {
        private readonly string[] _factorNames;
        private readonly int _expiredMinutes;

        public PreventDuplicateRequestsAttribute(int expiredMinutes, params string[] factorNames)
        {
            _expiredMinutes = expiredMinutes;
            _factorNames = factorNames;
        }

        public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
        {
            var filter = serviceProvider.GetService<PreventDuplicateRequestsActionFilter>();
            filter.FactorNames = _factorNames;
            filter.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(_expiredMinutes);
            return filter;
        }
        public bool IsReusable => false;
    }
}
